package cn.wolfcode.handler;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Slf4j
@Component
@CanalTable(value = "t_order_info")
public class OrderaInfoHandler implements EntryHandler<OrderInfo> {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void insert( OrderInfo orderInfo) {
        log.info("当有数据插入的时候会触发这个方法");
        //往redis中存储数据,防止redis重复下单
        redisTemplate.opsForSet().add(SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(orderInfo.getSeckillTime()+""),orderInfo.getUserId() + ":" + orderInfo.getSeckillId());
        //把订单放到redis中
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),orderInfo.getOrderNo(), JSON.toJSONString(orderInfo));
    }


    public void update(OrderInfo before,OrderInfo after) {
        log.info("当有数据更新的时候会触发这个方法");
        System.out.println(before);
        System.out.println(after);
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),after.getOrderNo(), JSON.toJSONString(after));
    }



    public void delete(OrderInfo orderInfo) {
        log.info("当有数据删除的时候会触发这个方法");
    }
}